package com.hdoit.kylin.logical.groupActivity.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.kylin.logical.baseInfo.model.Term;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.groupActivity.dao.IScheduleDao;
import com.hdoit.kylin.logical.groupActivity.model.Schedule;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.viewmodel.baseInfo.TermSearch;
import com.hdoit.kylin.logical.viewmodel.groupActivity.ScheduleSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;

/**
 * @author lufu
 * @email
 * @createtime 2017年08月02日 19:48:23
 * @comment
 */
@Component
public class ScheduleDaoImpl extends SuperDao<Schedule> implements IScheduleDao {

	@Override
	public List<Schedule> queryList(ScheduleSearch scheduleSearch,
			PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		finderExpression.select("schedule.*").from(Schedule.class, "schedule");

		appendFinder(scheduleSearch, finderExpression);

		if (pagerModel != null) {
			return queryList(finderExpression, scheduleSearch);
		}
		return queryList(finderExpression, scheduleSearch, pagerModel);
	}

	private void appendFinder(ScheduleSearch scheduleSearch,
			IFinderExpression finder) {
		if (scheduleSearch.getEmployeeIds() != null
				&& scheduleSearch.getEmployeeIds().size() > 0) {
			finder.andWhereInString("schedule.createId",
					scheduleSearch.getEmployeeIds());
		}
		if(scheduleSearch.getTermId() != null){
			finder.andWhere("schedule.termId = #{termId}")
			      .setParam("termId", scheduleSearch.getTermId());
		}
		if(scheduleSearch.getEmployeeId() != null){
			finder.andWhere("schedule.createId = #{employeeId}")
				  .setParam("employeeId", scheduleSearch.getEmployeeId());
		}
	}

	@Override
	public List<Schedule> queryList(ScheduleSearch scheduleSearch) {
		return queryList(scheduleSearch, null);
	}

	@Override
	public int queryCount(ScheduleSearch scheduleSearch) {
		IFinderExpression finderExpression = FinderExpressionFactory
				.createExpression();
		finderExpression.select("count(DISTINCT schedule.id) as count")
				.from(Schedule.class, "schedule");

		appendFinder(scheduleSearch, finderExpression);

		return queryCount(finderExpression, scheduleSearch);
	}

}